import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';

import '../../../../common/models/enums/speech_status.dart';


class SpeechButton extends StatefulWidget {
  const SpeechButton(
      {super.key, this.onTapDown, this.onTapUp, this.onTapCancel});

  final GestureTapDownCallback? onTapDown;
  final GestureTapUpCallback? onTapUp;
  final GestureTapCancelCallback? onTapCancel;

  @override
  State<StatefulWidget> createState() => _SpeechButtonState();
}

class _SpeechButtonState extends State<SpeechButton> {
  SpeechStatus _status = SpeechStatus.idle;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTapDown: (e) => ontapDown(e),
        onTapUp: (e) => ontapUp(e),
        onTapCancel: () => ontapCancel(),
        child: Container(
            height: 48,
            decoration: BoxDecoration(
                color: Theme.of(context).colorScheme.surface,
                border: Border.all(color: Theme.of(context).dividerColor),
                borderRadius: BorderRadius.circular(4)),
            child: _buttonView(context)));
  }

  Widget _buttonView(BuildContext context) {
    switch (_status) {
      case SpeechStatus.idle:
        return Center(child: Text('im_chat_tools_voice'.tr, style: const TextStyle(fontWeight: FontWeight.bold),));
      case SpeechStatus.speaking:
        return Center(
            child: Padding(
                padding: const EdgeInsets.only(left: 16, right: 16),
                child: Lottie.asset(
                    'assets/json/lottie-animation-voice-line.json',
                    delegates: _delegates(context),
                    height: 44,
                    fit: BoxFit.fill)));
      case SpeechStatus.thinking:
        return Center(
            child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Lottie.asset('assets/json/lottie-animation-loading-white.json',
                delegates: _delegates(context),
                width: 36,
                height: 36,
                fit: BoxFit.fill),
            const SizedBox(width: 10),
            Text('loading'.tr)
          ],
        ));
    }
  }
  /// 动画颜色
  LottieDelegates _delegates(BuildContext context) => LottieDelegates(values: [
        ValueDelegate.color(['**'],
            value: Theme.of(context).colorScheme.primary)
      ]);

  void ontapDown(TapDownDetails e) {
    setState(() {
      _status = SpeechStatus.speaking;
    });
    widget.onTapDown?.call(e);
  }

  void ontapUp(TapUpDetails e) {
    setState(() {
      _status = SpeechStatus.idle;
    });
    widget.onTapUp?.call(e);
  }

  void ontapCancel() {
    setState(() {
      _status = SpeechStatus.idle;
    });
    widget.onTapCancel?.call();
  }
}
